package com.cuckoo.cuckoo_app2.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cuckoo.cuckoo_app2.R;
import com.cuckoo.cuckoo_app2.dao.VipCard;
import com.cuckoo.cuckoo_app2.ui.VipMembershipAcitvity;

import java.util.List;

public class VipCardItemComponAdpter extends RecyclerView.Adapter<VipCardItemComponAdpter.ViewHolder> {

    private Context context;
    private List<VipCard> dataList;
    private VipMembershipAcitvity.ButtonClickListener buttonClickListener;


    public VipCardItemComponAdpter(Context context, List<VipCard> dataList, VipMembershipAcitvity.ButtonClickListener buttonClickListener) {
        this.context = context;
        this.dataList = dataList;
        this.buttonClickListener = buttonClickListener;

    }

    @NonNull
    @Override
    public VipCardItemComponAdpter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.vip_card_item_compon, parent, false);
        return new VipCardItemComponAdpter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        VipCard vipCard = dataList.get(position);

        holder.vip_card_item_title.setText(vipCard.getTitle());
        holder.vip_card_item_price.setText(vipCard.getPrice().toString());

        String number = "有效时间：" + vipCard.getDaysNumber().toString() + "天";
        holder.vip_card_item_days.setText(number);

        holder.vip_card_item_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                buttonClickListener.onEnter(vipCard);
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    // 清空数据并通知更新
    public void clearData() {
        this.dataList.clear(); // 假设 mData 不是 null
        notifyDataSetChanged();
    }

    // 更新数据列表的方法
    public void updateData(List<VipCard> dataList) {
        this.dataList = dataList; // 直接替换数据列表
        notifyDataSetChanged(); // 通知RecyclerView数据已更改
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView vip_card_item_price;
        TextView vip_card_item_title;
        TextView vip_card_item_days;
        Button vip_card_item_buy;

        ViewHolder(View itemView) {
            super(itemView);

            vip_card_item_price = itemView.findViewById(R.id.vip_card_item_price);
            vip_card_item_title = itemView.findViewById(R.id.vip_card_item_title);
            vip_card_item_days = itemView.findViewById(R.id.vip_card_item_days);
            vip_card_item_buy = itemView.findViewById(R.id.vip_card_item_buy);
        }
    }
}
